//
//  TopRoleSelectView.m
//  lhpc
//
//  Created by 李英杰 on 2017/4/6.
//  Copyright © 2017年 lhpc. All rights reserved.
//

#import "HomeTopView.h"
#define kUnComplete @"未提现"
#define kComplete @"已提现"

@interface HomeTopView ()

@property (nonatomic, strong) UIButton *unCompleteBtn;

@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UIButton *completeBtn;

@property (nonatomic, strong) UIView *redLineView;

@property (nonatomic, strong) UIButton *currentBtn;


@end

@implementation HomeTopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setMainView];
    }
    return self;
}


- (NSInteger)getCurrentIndex {
    NSInteger index = self.currentBtn == self.unCompleteBtn ? 0 : 1;
    return index;
}

-(void)setMainView{
    
    [self addSubview:self.unCompleteBtn];
    [self addSubview:self.line];
    [self addSubview:self.completeBtn];
    [self addSubview:self.redLineView];
    
    self.currentBtn = self.unCompleteBtn;
    [self setNeedsUpdateConstraints];
    
}

- (void)unCompleteActin:(UIButton *)sender {
    
    if (sender == self.currentBtn) {
        return;
    }
    
    self.currentBtn.selected = NO;
    sender.selected = YES;
    self.currentBtn = sender;
    
    [self moveBottomRedLine];
    
    if ([self.delegate respondsToSelector:@selector(unChangeToMoneyAction:)]) {
        [self.delegate unChangeToMoneyAction:sender];
    }
}

- (void)completeAction:(UIButton *)sender {
    
    if (sender == self.currentBtn) {
        return;
    }
    
    self.currentBtn.selected = NO;
    sender.selected = YES;
    self.currentBtn = sender;
    
    [self moveBottomRedLine];
    
    if ([self.delegate respondsToSelector:@selector(hasChangedToMoneyAction:)]) {
        [self.delegate hasChangedToMoneyAction:sender];
    }
}

- (void)moveBottomRedLine {
    [self.redLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(40, 2));
        make.centerX.mas_equalTo(self.currentBtn);
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
        [self layoutIfNeeded];
        if (self.superview) {
            [self.superview layoutIfNeeded];
        }
    }];
}

-(void)updateConstraints {
    [super updateConstraints];
    
    [self.unCompleteBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(self.mas_width).with.multipliedBy(0.5);
    }];
    
    [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.unCompleteBtn);
        make.left.mas_equalTo(self.unCompleteBtn.mas_right);
        make.size.mas_equalTo(CGSizeMake(0.5, 40));
    }];
    
    [self.completeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(0);
        make.width.mas_equalTo(self.mas_width).with.multipliedBy(0.5);
    }];
    
    [self.redLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.centerX.equalTo(self.unCompleteBtn);
        make.size.mas_equalTo(CGSizeMake(40, 2));
    }];
}

- (UIButton *)unCompleteBtn {
    if (!_unCompleteBtn) {
        _unCompleteBtn = [[UIButton alloc] init];
        [_unCompleteBtn setTitle:kUnComplete forState:UIControlStateNormal];
        [_unCompleteBtn setTitle:kUnComplete forState:UIControlStateSelected];
        
        [_unCompleteBtn setTitleColor:GRAYCOLOR forState:UIControlStateNormal];
        [_unCompleteBtn setTitleColor:ORANGCOLOR forState:UIControlStateSelected];
        _unCompleteBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_unCompleteBtn addTarget:self action:@selector(unCompleteActin:) forControlEvents:UIControlEventTouchUpInside];
        _unCompleteBtn.selected = YES;
    }
    return _unCompleteBtn;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = LINECOLOR;
    }
    return _line;
}

- (UIButton *)completeBtn {
    if (!_completeBtn) {
        _completeBtn = [[UIButton alloc] init];
        [_completeBtn setTitle:kComplete forState:UIControlStateNormal];
        [_completeBtn setTitle:kComplete forState:UIControlStateSelected];
        
        [_completeBtn setTitleColor:GRAYCOLOR forState:UIControlStateNormal];
        [_completeBtn setTitleColor:ORANGCOLOR forState:UIControlStateSelected];
        _completeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_completeBtn addTarget:self action:@selector(completeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _completeBtn;
}

- (UIView *)redLineView {
    if (!_redLineView) {
        _redLineView = [[UIView alloc] init];
        _redLineView.backgroundColor = HLHexColor(@"FF5500");
    }
    return _redLineView;
}


@end
